<template>
  <!-- 新增部门的弹层 -->
  <el-dialog title="新增部门" :visible="showDialog" @close="cancelFn">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form label-width="120px">
      <el-form-item label="部门名称">
        <el-input style="width: 80%" v-model="formData.name" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码">
        <el-input style="width: 80%" v-model="formData.code" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select @focus="getEmployeeSimple" style="width: 80%" v-model="formData.manager" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍">
        <el-input style="width: 80%" v-model="formData.introduce" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="cancelFn">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments } from '@/api/departments'
export default {
  data() {
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      peoples: []
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object
    }
  },
  methods: {
    cancelFn() {
      this.$emit('closeDK')
    },
    async getEmployeeSimple() {
      const { data: res } = await getEmployeeSimple()
      this.peoples = res.data
    },
    async btnOK() {
      const res = await addDepartments({ ...this.formData, pid: this.treeNode.id })
      console.log('Res:', res)
      this.cancelFn()
      this.formData = {}
      this.$emit('addDepts')
    }
  },
  created() {}
}
</script>
<style lang="scss" scoped></style>
